// pages/operat/codeManager/codeAllot/codeAllot.js
const LApp = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    codeAllotModel: {
      AllotType:0,
      AllotCount:0
    },
   
    directSubLevelCo: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   this.data.codeAllotModel.ApplyUserId = LApp.globalData.userInfo.UserID;
   this.data.codeAllotModel.F_EnterpriseName = LApp.globalData.userInfo.EnterpriseName;
   this.data.codeAllotModel.F_EnterpriseId = LApp.globalData.userInfo.EnterpriseId;
    this.getEnterpriseList();
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getGenCodeBatch();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
   wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 数据模型更改事件
   */
  bindModelChangeEvent: function (e) {
  
   wx.$$apply(e, this.data.codeAllotModel);
if (this.data.codeAllotModel.AllotType == 0) {
    if (this.data.codeAllotModel.StartCode && this.data.codeAllotModel.EndCode) {
     if (this.data.codeAllotModel.StartCode <=this.data.codeAllotModel.EndCode){
     this.data.codeAllotModel.AllotCount = (parseInt(this.data.codeAllotModel.EndCode) - parseInt(this.data.codeAllotModel.StartCode) + 1)
     this.setData({
      codeAllotModel: this.data.codeAllotModel
     })
     }
    }
}
   
  },
  enterpriseSelect: function (e) {
   if (this.data.directSubLevelCo.length > 0) {
    var _enterprise = this.data.directSubLevelCo[e.detail.value];
    if (_enterprise) {
     this.data.codeAllotModel.T_EnterpriseName = _enterprise.Name;
     this.data.codeAllotModel.T_EnterpriseId = _enterprise.Code;
     this.data.codeAllotModel.AllotProductName = undefined;
     this.data.codeAllotModel.ProductId = undefined;
      this.setData(this.data);
      this.getProductList(this.data.codeAllotModel.T_EnterpriseId);
    }
   }
  },
  productSelect:function(e){
   if (this.data.proudctList.length > 0) {
      var _product =  this.data.proudctList[e.detail.value];
      if(_product){
       this.data.codeAllotModel.AllotProductName = _product.Name;
       this.data.codeAllotModel.ProductId = _product.Code;
        this.setData(this.data);
      }
   }
  },
  radioButtonSelect:function(e){
    this.data.codeAllotModel.AllotType = e.detail.value;
   

    this.getGenCodeBatch();
    this.setData(this.data);
  },
  genCodeBatchSelect:function(e){
      var _gencodebatch = this.data.genCodeBatchList[e.detail.value];
      if(_gencodebatch){
        this.data.codeAllotModel.AllotBatchId = _gencodebatch.Code;
        this.data.codeAllotModel.AllotBatch = _gencodebatch.Name;
        var _this = this;
        if (this.data.codeAllotModel.AllotType == 1) {
         LApp._get({
          url: 'nbapi/codemanager/code/allot/add/codenums',
          data: { batchNo: _gencodebatch.Name},
          success: function (r) {
           _this.data.codeAllotModel.AllotCount = r;           
           _this.setData({
            codeAllotModel: _this.data.codeAllotModel
           });
          }
         })
        }
        this.setData({
          codeAllotModel: this.data.codeAllotModel
        });
      }
    
    
  },
  /**
   * 获取企业列表
   */
  getEnterpriseList: function () {
    let _this = this;
    LApp._get({
      url: "nbapi/comm/get/enterprise/direct/sub/level",
      data: { enterpriseId: LApp.globalData.userInfo.EnterpriseId },
      success: function (r) {
        _this.setData({
          directSubLevelCo: r
        })
       
      }
    })
  },
  getProductList: function (__enterpriseId) {
    let _this = this;
    LApp._get({
      url: 'nbapi/product/selectitem',
      data: { enterpriseId: __enterpriseId },
      success: function (r) {
        _this.setData({
         proudctList:r
       })
      }
    })
  },
  getBrandList:function(__productId){

  },
  getGenCodeBatch:function(){
    let _this = this;
    LApp._get({
      url:'nbapi/codemanager/code/allot/codebatch/get',
      data: { enterpriseId: LApp.globalData.userInfo.EnterpriseId, allottype: this.data.codeAllotModel.AllotType},
      success:function(r){
          _this.setData({
            genCodeBatchList:r
          })
      }
    })
  },
  
  saveCodeAllot:function(){
   var _model = this.data.codeAllotModel;
   if (!_model.T_EnterpriseId||!_model.AllotBatchId){
     wx.showModal({
       title: '提示',
       content: '必填',
       showCancel:false
     })
     return ;
   }
   if(_model.AllotType == 0&&(!_model.StartCode||!_model.EndCode)){
     wx.showModal({
       title: '提示',
       content: '起始/结束码必缺失',
       showCancel: false
     })
     return;
   }
   LApp._post({
     url:'nbapi/codemanager/code/allot/add',
     data: this.data.codeAllotModel,
     success:function(r){
     
      wx.showToast({
        title: '分配成功',
        icon:"success",
        
       success: function () {
        //刷新父级列表
        wx.$$getParent().getUserCodeAllotList();
        wx.navigateBack({
         delta: 1
        })
       }
      })
     }
   })
  }
})